package org.jeecg.modules.oms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.oms.service.IOmsStatisticsService;
import org.jeecg.modules.oms.vo.CustomerStatisticsVO;
import org.jeecg.modules.oms.vo.SalesOverviewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: OMS销售统计控制器
 * @Author: jeecg-boot
 * @Date: 2025-01-13
 * @Version: V1.0
 */
@Api(tags = "OMS销售统计")
@RestController
@RequestMapping("/oms/statistics")
@Slf4j
public class OmsStatisticsController {

    @Autowired
    private IOmsStatisticsService omsStatisticsService;

    /**
     * 获取销售概览统计数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param request   请求对象
     * @return 销售概览统计数据
     */
    @AutoLog(value = "销售概览统计-查询")
    @ApiOperation(value = "销售概览统计-查询", notes = "销售概览统计-查询")
    @GetMapping(value = "/sales/overview")
    public Result<SalesOverviewVO> getSalesOverview(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            SalesOverviewVO result = omsStatisticsService.getSalesOverview(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取销售概览统计数据失败", e);
            return Result.error("获取销售概览统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取销售趋势数据
     *
     * @param period 统计周期 (day/week/month/year)
     * @param limit  返回数据条数
     * @param request 请求对象
     * @return 销售趋势数据
     */
    @AutoLog(value = "销售趋势统计-查询")
    @ApiOperation(value = "销售趋势统计-查询", notes = "销售趋势统计-查询")
    @GetMapping(value = "/sales/trend")
    public Result<?> getSalesTrend(
            @RequestParam(value = "period", defaultValue = "month") String period,
            @RequestParam(value = "limit", defaultValue = "12") Integer limit,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getSalesTrend(period, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取销售趋势数据失败", e);
            return Result.error("获取销售趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取订单状态分布统计
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @param request   请求对象
     * @return 订单状态分布数据
     */
    @AutoLog(value = "订单状态分布统计-查询")
    @ApiOperation(value = "订单状态分布统计-查询", notes = "订单状态分布统计-查询")
    @GetMapping(value = "/order/status-distribution")
    public Result<?> getOrderStatusDistribution(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getOrderStatusDistribution(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取订单状态分布统计失败", e);
            return Result.error("获取订单状态分布统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取客户概览统计数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param request   请求对象
     * @return 客户概览统计数据
     */
    @AutoLog(value = "客户概览统计-查询")
    @ApiOperation(value = "客户概览统计-查询", notes = "客户概览统计-查询")
    @GetMapping(value = "/customer/overview")
    public Result<CustomerStatisticsVO.CustomerOverviewVO> getCustomerOverview(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            CustomerStatisticsVO.CustomerOverviewVO result = omsStatisticsService.getCustomerOverview(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取客户概览统计数据失败", e);
            return Result.error("获取客户概览统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取客户销售金额排行榜
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param limit     返回数据条数
     * @param request   请求对象
     * @return 客户销售金额排行榜
     */
    @AutoLog(value = "客户销售金额排行-查询")
    @ApiOperation(value = "客户销售金额排行-查询", notes = "客户销售金额排行-查询")
    @GetMapping(value = "/customer/sales-ranking")
    public Result<?> getCustomerSalesRanking(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getCustomerSalesRanking(startDate, endDate, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取客户销售金额排行榜失败", e);
            return Result.error("获取客户销售金额排行榜失败: " + e.getMessage());
        }
    }

    /**
     * 获取客户订单数量排行榜
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param limit     返回数据条数
     * @param request   请求对象
     * @return 客户订单数量排行榜
     */
    @AutoLog(value = "客户订单数量排行-查询")
    @ApiOperation(value = "客户订单数量排行-查询", notes = "客户订单数量排行-查询")
    @GetMapping(value = "/customer/order-ranking")
    public Result<?> getCustomerOrderRanking(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getCustomerOrderRanking(startDate, endDate, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取客户订单数量排行榜失败", e);
            return Result.error("获取客户订单数量排行榜失败: " + e.getMessage());
        }
    }

    /**
     * 获取客户贡献度分析数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param type      分析类型 (sales/orders)
     * @param request   请求对象
     * @return 客户贡献度分析数据
     */
    @AutoLog(value = "客户贡献度分析-查询")
    @ApiOperation(value = "客户贡献度分析-查询", notes = "客户贡献度分析-查询")
    @GetMapping(value = "/customer/contribution")
    public Result<CustomerStatisticsVO.CustomerContributionVO> getCustomerContribution(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "type", defaultValue = "sales") String type,
            HttpServletRequest request) {
        try {
            CustomerStatisticsVO.CustomerContributionVO result = omsStatisticsService.getCustomerContribution(startDate, endDate, type);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取客户贡献度分析数据失败", e);
            return Result.error("获取客户贡献度分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取新老客户分析数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param type      分析类型 (count/sales)
     * @param request   请求对象
     * @return 新老客户分析数据
     */
    @AutoLog(value = "新老客户分析-查询")
    @ApiOperation(value = "新老客户分析-查询", notes = "新老客户分析-查询")
    @GetMapping(value = "/customer/analysis")
    public Result<CustomerStatisticsVO.CustomerAnalysisVO> getCustomerAnalysis(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "type", defaultValue = "count") String type,
            HttpServletRequest request) {
        try {
            CustomerStatisticsVO.CustomerAnalysisVO result = omsStatisticsService.getCustomerAnalysis(startDate, endDate, type);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取新老客户分析数据失败", e);
            return Result.error("获取新老客户分析数据失败: " + e.getMessage());
        }
    }

    // ==================== 产品销售分析相关接口 ====================

    /**
     * 获取产品销售概览统计数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param request   请求对象
     * @return 产品销售概览统计数据
     */
    @AutoLog(value = "产品销售概览统计-查询")
    @ApiOperation(value = "产品销售概览统计-查询", notes = "产品销售概览统计-查询")
    @GetMapping(value = "/product/overview")
    public Result<?> getProductOverview(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getProductOverview(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取产品销售概览统计数据失败", e);
            return Result.error("获取产品销售概览统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取产品系列销售排行榜
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param limit     返回数据条数
     * @param request   请求对象
     * @return 产品系列销售排行榜
     */
    @AutoLog(value = "产品系列销售排行-查询")
    @ApiOperation(value = "产品系列销售排行-查询", notes = "产品系列销售排行-查询")
    @GetMapping(value = "/product/series-ranking")
    public Result<?> getProductSeriesRanking(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getProductSeriesRanking(startDate, endDate, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取产品系列销售排行榜失败", e);
            return Result.error("获取产品系列销售排行榜失败: " + e.getMessage());
        }
    }

    /**
     * 获取产品颜色销售分析数据
     *
     * @param startDate  开始日期 (格式: yyyy-MM-dd)
     * @param endDate    结束日期 (格式: yyyy-MM-dd)
     * @param seriesName 产品系列筛选
     * @param request    请求对象
     * @return 产品颜色销售分析数据
     */
    @AutoLog(value = "产品颜色销售分析-查询")
    @ApiOperation(value = "产品颜色销售分析-查询", notes = "产品颜色销售分析-查询")
    @GetMapping(value = "/product/color-analysis")
    public Result<?> getProductColorAnalysis(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "seriesName", required = false) String seriesName,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getProductColorAnalysis(startDate, endDate, seriesName);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取产品颜色销售分析数据失败", e);
            return Result.error("获取产品颜色销售分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取客户区域销售分析数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param level     区域级别 (province/city/district)
     * @param request   请求对象
     * @return 客户区域销售分析数据
     */
    @AutoLog(value = "客户区域销售分析-查询")
    @ApiOperation(value = "客户区域销售分析-查询", notes = "客户区域销售分析-查询")
    @GetMapping(value = "/product/region-analysis")
    public Result<?> getProductRegionAnalysis(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "level", defaultValue = "province") String level,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getProductRegionAnalysis(startDate, endDate, level);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取客户区域销售分析数据失败", e);
            return Result.error("获取客户区域销售分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取产品销售趋势分析数据
     *
     * @param period    统计周期 (day/week/month/year)
     * @param limit     返回数据点数量
     * @param dimension 分析维度 (sales/orders/quantity)
     * @param request   请求对象
     * @return 产品销售趋势分析数据
     */
    @AutoLog(value = "产品销售趋势分析-查询")
    @ApiOperation(value = "产品销售趋势分析-查询", notes = "产品销售趋势分析-查询")
    @GetMapping(value = "/product/trend")
    public Result<?> getProductTrend(
            @RequestParam(value = "period", defaultValue = "month") String period,
            @RequestParam(value = "limit", defaultValue = "12") Integer limit,
            @RequestParam(value = "dimension", defaultValue = "sales") String dimension,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getProductTrend(period, limit, dimension);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取产品销售趋势分析数据失败", e);
            return Result.error("获取产品销售趋势分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取产品销售分布图表数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param type      分布类型 (series/color/region)
     * @param request   请求对象
     * @return 产品销售分布图表数据
     */
    @AutoLog(value = "产品销售分布图表-查询")
    @ApiOperation(value = "产品销售分布图表-查询", notes = "产品销售分布图表-查询")
    @GetMapping(value = "/product/distribution")
    public Result<?> getProductDistribution(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "type", defaultValue = "series") String type,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getProductDistribution(startDate, endDate, type);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取产品销售分布图表数据失败", e);
            return Result.error("获取产品销售分布图表数据失败: " + e.getMessage());
        }
    }

    // ==================== 区域销售分析相关接口 ====================

    /**
     * 获取区域销售概览统计数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param level     区域级别 (province/city/area)
     * @param request   请求对象
     * @return 区域销售概览统计数据
     */
    @AutoLog(value = "区域销售概览统计-查询")
    @ApiOperation(value = "区域销售概览统计-查询", notes = "区域销售概览统计-查询")
    @GetMapping(value = "/region/overview")
    public Result<?> getRegionOverview(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "level", defaultValue = "province") String level,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getRegionOverview(startDate, endDate, level);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取区域销售概览统计数据失败", e);
            return Result.error("获取区域销售概览统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取区域销售分布数据
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param level     区域级别 (province/city/area)
     * @param dataType  数据类型 (sales/orders/customers)
     * @param request   请求对象
     * @return 区域销售分布数据
     */
    @AutoLog(value = "区域销售分布-查询")
    @ApiOperation(value = "区域销售分布-查询", notes = "区域销售分布-查询")
    @GetMapping(value = "/region/distribution")
    public Result<?> getRegionDistribution(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "level", defaultValue = "province") String level,
            @RequestParam(value = "dataType", defaultValue = "sales") String dataType,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getRegionDistribution(startDate, endDate, level, dataType);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取区域销售分布数据失败", e);
            return Result.error("获取区域销售分布数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取区域销售排行榜
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate   结束日期 (格式: yyyy-MM-dd)
     * @param level     区域级别 (province/city/area)
     * @param limit     返回数据条数
     * @param request   请求对象
     * @return 区域销售排行榜
     */
    @AutoLog(value = "区域销售排行-查询")
    @ApiOperation(value = "区域销售排行-查询", notes = "区域销售排行-查询")
    @GetMapping(value = "/region/ranking")
    public Result<?> getRegionRanking(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "level", defaultValue = "province") String level,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getRegionRanking(startDate, endDate, level, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取区域销售排行榜失败", e);
            return Result.error("获取区域销售排行榜失败: " + e.getMessage());
        }
    }

    /**
     * 获取区域增长率对比数据
     *
     * @param period    统计周期 (month/quarter/year)
     * @param level     区域级别 (province/city/area)
     * @param limit     返回数据条数
     * @param request   请求对象
     * @return 区域增长率对比数据
     */
    @AutoLog(value = "区域增长率对比-查询")
    @ApiOperation(value = "区域增长率对比-查询", notes = "区域增长率对比-查询")
    @GetMapping(value = "/region/growth")
    public Result<?> getRegionGrowth(
            @RequestParam(value = "period", defaultValue = "month") String period,
            @RequestParam(value = "level", defaultValue = "province") String level,
            @RequestParam(value = "limit", defaultValue = "8") Integer limit,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getRegionGrowth(period, level, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取区域增长率对比数据失败", e);
            return Result.error("获取区域增长率对比数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取区域销售趋势数据
     *
     * @param period    统计周期 (month/quarter/year)
     * @param level     区域级别 (province/city/area)
     * @param limit     返回数据点数量
     * @param request   请求对象
     * @return 区域销售趋势数据
     */
    @AutoLog(value = "区域销售趋势-查询")
    @ApiOperation(value = "区域销售趋势-查询", notes = "区域销售趋势-查询")
    @GetMapping(value = "/region/trend")
    public Result<?> getRegionTrend(
            @RequestParam(value = "period", defaultValue = "month") String period,
            @RequestParam(value = "level", defaultValue = "province") String level,
            @RequestParam(value = "limit", defaultValue = "12") Integer limit,
            HttpServletRequest request) {
        try {
            Object result = omsStatisticsService.getRegionTrend(period, level, limit);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取区域销售趋势数据失败", e);
            return Result.error("获取区域销售趋势数据失败: " + e.getMessage());
        }
    }
}
